package com.yidu.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.common.Tools;
import com.yidu.dao.ListenAuditingMapper;
import com.yidu.dao.ListenProductionMapper;
import com.yidu.dao.ListenPurchaseMapper;
import com.yidu.domain.ListenAuditing;
import com.yidu.domain.ListenBackstageUser;
import com.yidu.domain.ListenProduction;
import com.yidu.domain.ListenPurchase;
import com.yidu.service.ListenAuditingService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangyu
 * @since 2018-11-27
 */
@Service
public class ListenAuditingServiceImpl implements ListenAuditingService {
	@Resource
	ListenAuditingMapper auditingMapper;
	
	@Resource
	ListenPurchaseMapper purDao;
	
	@Resource
	ListenProductionMapper prodDao;
	@Override
	public int insertPurchaseAudit(ListenAuditing listenAuditing) {
		if(listenAuditing.getAuditingStatus()==2) {
			listenAuditing.setAuditingStatus(1);//分店财务已审核
		}else if(listenAuditing.getAuditingStatus()==4) {
			listenAuditing.setAuditingStatus(0);//审核不通过
		}else if(listenAuditing.getAuditingStatus()==3) {
			listenAuditing.setAuditingStatus(2);//总经理已审核
		}
		listenAuditing.setAuditingTime(new Date());
		listenAuditing.setAuditingId(Tools.getRandomString(32));
		listenAuditing.setIsva(1);
		return auditingMapper.insertSelective(listenAuditing);
	}
	
	@Override
	public List<ListenAuditing> selectMusicPurchaseAuditDetail(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return auditingMapper.selectMusicPurchaseAuditDetail(map);
	}
	@Override
	public int selectRows(Map<String, Object> map) {
		
		return auditingMapper.selectRows(map);
	}
	/**
	 * 增加批发审核记录
	 */
	@Override
	public int pfInsert(ListenAuditing auditing) {
		// TODO Auto-generated method stub
		return auditingMapper.insertSelective(auditing);
	}
	/**
	 * 批发审核根据id查看所有的方法
	 */
	@Override
	public List<ListenAuditing> idSelect(Map<String, Object> map) {
		// 调用批发审核根据id查看所有的方法
		return auditingMapper.idSelect(map);
	}
	/**
	 * 批发审核 根据id查看所有行数的方法
	 */
	@Override
	public int idCount(Map<String, Object> map) {
		// 调用批发审核根据id查看所有行数的方法
		return auditingMapper.idCount(map);
	}
	
	

/**
	 * 增加批发审核记录
	 */
	@Override
	public int sellInsert(ListenAuditing auditing) {
		if(auditing.getAuditingStatus()==2) {
			auditing.setAuditingStatus(1);//分店财务已审核
		}else if(auditing.getAuditingStatus()==4) {
			auditing.setAuditingStatus(0);//审核不通过
		}else if(auditing.getAuditingStatus()==3) {
			auditing.setAuditingStatus(2);//总经理已审核
		}
		return auditingMapper.insertSelective(auditing);
	}

	@Override
	public List<ListenAuditing> findAuditing(Map<String, Object> map) {
		 
		return auditingMapper.findAuditing(map);
	}

	@Override
	public int findAuditingCount(Map<String, Object> map) {
		
		return auditingMapper.findAuditingCount(map);
	}

	@Override
	public int addMaterialAuditing(ListenAuditing au,HttpSession session) {
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		int rows=0;
		ListenPurchase pur=new ListenPurchase();
		if(au.getMan()==1) {
			if(au.getAuditingStatus()==1) {
				pur.setPurchaseId(au.getVocationalWorkId());
				pur.setAuditingStatus(2);
				
				au.setAuditingStatus(1);
			}else if(au.getAuditingStatus()==2) {
				pur.setPurchaseId(au.getVocationalWorkId());
				pur.setAuditingStatus(4);
				
				au.setAuditingStatus(3);
			} 
		}else {
			if(au.getAuditingStatus()==1) {
				pur.setPurchaseId(au.getVocationalWorkId());
				pur.setAuditingStatus(3);
				
				au.setAuditingStatus(2);
			}else if(au.getAuditingStatus()==2) {
				pur.setPurchaseId(au.getVocationalWorkId());
				pur.setAuditingStatus(4);
				
				au.setAuditingStatus(3);
			} 
		}
		
		rows=purDao.updateByPrimaryKeySelective(pur);
		
		 
		au.setAuditingId(Tools.getRandomString(32));
		au.setAuditingTime(new Date());
		au.setAuditingPeople(users.getBuName());
		rows=auditingMapper.insertSelective(au);
		
		return rows;
	}

	@Override
	public int addProductionAuditing(ListenAuditing au,HttpSession session) {
		ListenBackstageUser users = (ListenBackstageUser) session.getAttribute("user");
		int rows=0;
		ListenProduction prod=new ListenProduction();
		if(au.getMan()==1) {
			if(au.getAuditingStatus()==1) {
				prod.setProdId(au.getVocationalWorkId());
				prod.setProdPermit(2);
				
				au.setAuditingStatus(1);
			}else if(au.getAuditingStatus()==2) {
				prod.setProdId(au.getVocationalWorkId());
				prod.setProdPermit(4);
				
				au.setAuditingStatus(3);
			} 
		}else {
			if(au.getAuditingStatus()==1) {
				prod.setProdId(au.getVocationalWorkId());
				prod.setProdPermit(3);
				
				au.setAuditingStatus(2);
			}else if(au.getAuditingStatus()==2) {
				prod.setProdId(au.getVocationalWorkId());
				prod.setProdPermit(3);
				
				au.setAuditingStatus(4);
			} 
		}
		
		rows=prodDao.updateByPrimaryKeySelective(prod);
		
		 
		au.setAuditingId(Tools.getRandomString(32));
		au.setAuditingTime(new Date());
		au.setAuditingPeople(users.getBuName());
		rows=auditingMapper.insertSelective(au);
		
		return rows;
	}

}
